<template>
  <div id="enterprise">
    <nav>
      <el-input placeholder="输入关键字" style="width:200px" v-model="keyword"></el-input>
      <el-button type="primary" @click="getBrandList">检索</el-button>
      <el-button type="primary" @click="showDialog">添加</el-button>
    </nav>

    <el-table
      class="table-first"
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="date"
        label="品牌头像"
        width="180">
        <template slot-scope="scope">
          <div>

            <img :src="scope.row.img_url" alt="" style="width:60px;border-radius:50%;height:60px;object-fit:cover">
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="品牌名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序">
      </el-table-column>
      <el-table-column
        prop="created_at"
        label="创建时间">
      </el-table-column>
      <el-table-column prop="link_url" label="链接" width="100" sortable>
        <template slot-scope="scope">
          <div>
            <el-button type="primary"
                       v-clipboard:copy="scope.row.share_url"
                       v-clipboard:success="onCopy"
                       size="mini"
                       v-clipboard:error="onError">复制链接
            </el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="360">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="editorPerson(scope.row.id)">编辑</el-button>
          <el-button type="danger" class="danger-btn" size="mini" @click="delBrand(scope.row.id)">删除</el-button>
          <el-button type="Success" size="mini" @click="showDetailDialog(scope.row.id)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-col :span="24" class="footer-bar">
      <div class="page-code">
        <el-pagination
          background
          :page-size="16"
          layout="prev, pager, next"
          :total="totalPage"
          @current-change="pageChange"
        >
        </el-pagination>
      </div>
    </el-col>


    <!-- 添加，编辑 -->
    <el-dialog :title="dialogTit" :visible.sync="addFormVisible" @close="initAddform" width="70%"
               :close-on-click-modal="false">
      <el-form label-width="110px" label-position="left">
        <el-form-item label="品牌头像">
          <el-upload
            class="avatar-uploader"
            action="https://wxapp.gzmmkj.cn/api/file/upload-img"
            name="photo"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="addForm.brandImg" :src="addForm.brandImg" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="品牌名称">
          <el-input v-model="addForm.brandName" auto-complete="off" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="品牌描述">
          <el-input v-model="addForm.desc" type="textarea" auto-complete="off" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="小程序码描述">
          <el-input v-model="addForm.codeText" type="textarea" maxlength="11" placeholder="字数不能超过11个"
                    auto-complete="off" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="addForm.sortNum" auto-complete="off" style="width:300px" type="number"></el-input>
        </el-form-item>
        <el-form-item label="置顶视频/图片" prop="Video">

          <audioPicUpload v-model="topContent" v-if="topContentShow"></audioPicUpload>
        </el-form-item>

        <editor v-model="addContent" v-if="editorShow"></editor>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="setBrand">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="详情" :visible.sync="detailFormVisible" @close="initDetailform" class="detailDialog">
      <el-form>
        <el-form-item>
          <div v-if="topContent.type=='img'">
            <img :src="topContent.src" alt="">
          </div>
          <div v-if="topContent.type=='video'">
            <video :src="topContent.src" controls style="width:100%;height:300px"
                   :poster="topContent.video_post_src"></video>
          </div>
        </el-form-item>
        <el-form-item>
          <div v-html="addContent"></div>
        </el-form-item>
      </el-form>

    </el-dialog>
  </div>
</template>
<script>
  import PcCode from '../../assets/json/pc-code.json';
  import audioPicUpload from '@/components/audioPicUpload';
  import editor from '@/components/editor';

  export default {
    name: 'person',
    components: {
      editor,
      audioPicUpload,

    },
    data() {
      return {
        keyword: '',
        id: '',//数据id
        page: 1,
        totalPage: 0,
        PcCode,
        dialogTit: '',
        addFormVisible: false,
        addContent: '',//富文本
        editorShow: true,//显示富文本

        topType: '',//区分置顶的是图片还是视频
        uploadUrl: '',//
        addForm: {
          desc: '',
          brandImgId: '',
          brandImg: '',
          brandName: '',
          sortNum: '',//排序
          codeText: ''//小程序描述
        },
        topContent: {//置顶的信息

        },
        seletOptions: [
          {
            value: '选项1',
            label: '人物榜'
          },
          {
            value: '选项2',
            label: '人物云集'
          }
        ],
        topContentShow: true,
        tableData: [],
        detailFormVisible: false//详情页的显示
      }
    },
    methods: {

      onCopy(e) {
        this.$message({
          type: 'success',
          message: '复制成功'
        })
      },
      onError(erro) {
        this.$message({
          type: 'error',
          message: '复制失败'
        })
      },
      showDialog() {
        //新增
        this.addFormVisible = true;
      },
      addPerson() {
        //提交表单
        this.addFormVisible = false;
        this.initAddform();
      },
      editorPerson(id) {
        //编辑
        this.id = id;
        this.getBrand();
        this.addFormVisible = true;

      },
      async getBrand() {
        let res = await this.$api.getBrand({id: this.id});
        if (res.data.level == 'success') {
          let data = res.data.data
          this.addForm.brandImgId = data.thumb_id;
          this.addForm.brandImg = data.img_url;
          this.addForm.brandName = data.name;
          this.addForm.desc = data.desc;
          this.addForm.sortNum = data['sort'];
          this.addForm.codeText = data.qr_title;
          if (data.video_url) {
            this.topContent = {
              src: data.video_url,
              srcId: data.video_id,
              type: 'video',
              video_post_src: data.video_img_url,
              video_post_id: data.video_img_id,
            }
          } else if
          (data.top_url) {
            this.topContent = {
              src: data.top_url,
              srcId: data.top_id,
              type: 'img',

            }
          }
          this.addContent = data.content;
          this.editorShow = false;
          setTimeout(() => {
            this.editorShow = true;
          }, 500)
        }

      },
      delBrand(id) {
        //删除
        const judge = this.$confirm('删除该品牌?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const res = await this.$api.delBrand({id});
          if (res.data.level == 'success') {
            this.getBrandList();
            this.$message({
              type: 'success',
              message: res.data.message
            });
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }

        }).catch(() => {

        });
      },
      showDetailDialog(id) {
        //查看详情
        this.id = id
        this.detailFormVisible = true;
        this.getBrand()
      },
      handleAvatarSuccess(res, file) {
        //上传照片成功
        if (res.data) {
          this.addForm.brandImg = res.data.img_url;
          this.addForm.brandImgId = res.data.id;
          this.$forceUpdate()
        }
      },
      initAddform() {
        //初始化数据
        this.addContent = '';
        this.editorShow = false;
        this.topContentShow = false;
        setTimeout(() => {
          this.editorShow = true;
          this.topContentShow = true;
        }, 500)
        this.addForm = {
          brandImgId: '',
          desc: '',
          brandImg: '',
          brandName: '',
          sortNum: '',//排序
          address: '',//地区
          codeText: ''
        };
        this.id = '';
        this.topContent = {}
      },
      initDetailform() {
        //初始化查看详情
        this.initAddform()
      },
      closeDialog() {
        this.addFormVisible = false;
        this.initAddform();
      },
      async getBrandList() {
        let params = {
          page: this.page,
          keyword: this.keyword,
          limit: 16
        }
        let res = await this.$api.getBrandList(
          params);
        if (res.data.level == 'success') {
          this.tableData = res.data.data;
          this.totalPage = res.data.page_info.total
        }
      },
      async setBrand() {
        //新增修改
        var top_id = '', video_id = '', video_img_id;
        if (this.topContent.type == 'video') {
          video_id = this.topContent.srcId;
          video_img_id = this.topContent.video_post_id;
        } else {
          top_id = this.topContent.srcId;

        }
        let data = this.addForm
        let params = {
          img_id: data.brandImgId,
          name: data.brandName,
          sort: data.sortNum,
          video_id: video_id,
          top_id: top_id,
          video_img_id: video_img_id,
          desc: data.desc,
          content: this.addContent,
          id: this.id,
          qr_title: data.codeText
        }
        let res = await this.$api.setBrand(params);
        if (res.data.level == 'success') {
          this.$message({
            message: res.data.message,
            type: 'success'
          });
          this.addFormVisible = false;
          this.getBrandList();
        } else {
          this.$message({
            message: res.data.message,
            type: 'error'
          });
        }
      },
      pageChange(e) {
        //翻页
        this.page = e;
        this.getBrandList();
      },

    },
    mounted() {
      // setTimeout(()=>{
      //      this.topContent={
      //         src:'http://file.rzkeji.com//web/shanghui/uploads/2019/8/20/f02a683c3f6f340851dda5added72d90.jpg',
      //         srcId:1,
      //         type:'img'
      //     }
      // },2000)
      this.getBrandList()
    },
  }
</script>
<style lang="scss" scoped>
  .footer-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #f2f2f2;
    padding: 8px;
    margin: 10px 0px;
    box-sizing: border-box;
  }

  .dialog-footer {
    text-align: center;
  }

  nav {
    background: #eee;
    padding: 20px;
    // position: fixed;
    width: 100%;
    z-index: 99;
  }

  // .table-first{
  //     padding-top:100px;
  // }

  //上传图片样式
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
    border: 1px dashed #d9d9d9;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
    object-fit: contain;
  }
</style>
<style>
  .detailDialog img {
    max-width: 300px;
  }
</style>
